﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;

namespace Viewer.NhanSu.UserControl
{
    public partial class UCNgachBacLuong : System.Web.UI.UserControl
    {

        NSNgachBacLuong ngach = new NSNgachBacLuong();
        private List<NSNgachBacLuong> lst;
        NSNhanVien nhanvien = new NSNhanVien();
        CommonCode common = new CommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void ResetControl()
        {
            txtCodeNgach.Text = string.Empty;
            txtTenNgach.Text = string.Empty;
            hdNgachCongChucId.Text = string.Empty;
            txtHeSo.Text = string.Empty;
            cbbBacLuong.SelectedItem.Value = string.Empty;
            txtGhiChu.Text = string.Empty;
            txtLyDo.Text = string.Empty;
            
        }
        [DirectMethod]
        public void LookUp_TimNgach()
        {
            UCTimNgach.ShowDialogTimNgach(common.NgachCongChuc);
            X.Mask.Hide();
            
        }
        [DirectMethod]
        public void ShowDialog(string type, string hdId, string nhanvienID, string typeParent)
        {

            storeBacLuong.DataSource = common.LoadDataCommonByCodeKey(common.BacLuong).OrderBy(a => a.CodeID);
            storeBacLuong.DataBind();

            hdNhanVienId.Text = nhanvienID;
            hdNgachId.Text = hdId;
            if (type.Equals("AddNew"))
            {
                wdNgachBacLuong.Title = "Tạo mới ngạch công chức";
                ResetControl();
                hdType.Text = type;
                btnSave.Hidden = false;
                hdParentType.Text = typeParent;
            }
            else if (type.Equals("Edit"))
            {
                wdNgachBacLuong.Title = "Sửa đổi ngạch công chức";
                hdType.Text = type;
                btnSave.Hidden = false;
                LoadDataNBL(Convert.ToInt16(hdId));
            }
            else if (type.Equals("View"))
            {
                wdNgachBacLuong.Title = "Xem chi tiết ngạch công chức";
                hdType.Text = type;
                btnSave.Hidden = true;
                LoadDataNBL(Convert.ToInt16(hdId));
            }
            wdNgachBacLuong.Show();
        }
        public void LoadDataNBL(int id)
        {
            lst = (List<NSNgachBacLuong>)Session["NBL"];
            NSNgachBacLuong item = lst.Where(a => a.NgachId.Equals(id)).SingleOrDefault();
            dtNgayHieuLuc.Text = item.NgayHieuLuc!=null?((DateTime)item.NgayHieuLuc).ToString("dd/MM/yyyy"):string.Empty;
            txtGhiChu.Text = item.GhiChu;

            txtTenNgach.Text = item.TenNgach.ToString();
            txtCodeNgach.Text = item.MaNgach.ToString();
            txtHeSo.Text = item.HeSo>0 ? item.HeSo.ToString() : string.Empty;
            cbbBacLuong.SelectedItem.Value = item.BacLuongID.ToString();
        }
        protected void btnSaveNBL_CLick(object sender, DirectEventArgs e)
        {
            DateTime ngayHieuLuc = Convert.ToDateTime(dtNgayHieuLuc.Text);
            int nhanVienId = !string.IsNullOrEmpty(hdNhanVienId.Text) ? Convert.ToInt16(hdNhanVienId.Text) : 0;
            string ngachCongChuc = !string.IsNullOrEmpty(txtTenNgach.Text) ? txtTenNgach.Text : string.Empty;
            string maNgach = !string.IsNullOrEmpty(txtCodeNgach.Text) ? txtCodeNgach.Text : string.Empty;
            int bacLuongId = !string.IsNullOrEmpty(cbbBacLuong.SelectedItem.Value) ? Convert.ToInt16(cbbBacLuong.SelectedItem.Value) : 0;
            decimal heSoId = !string.IsNullOrEmpty(txtHeSo.Text) ? Convert.ToDecimal(txtHeSo.Text) : 0;

            if (hdType.Text.Equals("AddNew"))
            {
                NSNgachBacLuong item = ngach.GetNgachBacLuong(nhanVienId, ngachCongChuc, maNgach, bacLuongId, heSoId, ngayHieuLuc, txtGhiChu.Text,txtLyDo.Text);
                ((UCNhanVien)this.Parent).GetNgachBacLuong(item);
            }
            else if (hdType.Text.Equals("Edit"))
            {
                ngach.EditNBL(Convert.ToInt16(hdNgachId.Text), ngachCongChuc, maNgach, bacLuongId, heSoId, ngayHieuLuc, txtGhiChu.Text,txtLyDo.Text);
                ((UCNhanVien)this.Parent).ReloadGrid("NBL");
            }
            X.Mask.Hide();
            wdNgachBacLuong.Hide();

        }
        [DirectMethod]
        public void FindCommonName_NgachCongChuc(string codeString)
        {
            CHCommonCode obj = common.LoadCommonCodeByCodeString(codeString, "NgachCongChuc");
            if (obj != null)
            {
                txtTenNgach.Text = obj.CodeValue;
                hdNgachCongChucId.Text = obj.CodeID.ToString();

            }
            else
            {
                txtTenNgach.Text = string.Empty;
                hdNgachCongChucId.Text = string.Empty;
            }

        }
    }
}